package pers.qianyu.month_202012.date_20201206;

import org.junit.*;

import java.util.*;

/**
 * [Leetcode 每日一题]
 * 118. 杨辉三角
 * https://leetcode-cn.com/problems/pascals-triangle/
 *
 * @author mizzle rain
 * @date 2020-12-06 13:00
 */
public class YangHui {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 1; i <= numRows; i++) {
            List<Integer> list = new ArrayList<>();
            res.add(list);
        }
        List<Integer> pre = null;
        for (int i = 0; i < numRows; i++) {
            List<Integer> list = res.get(i);
            for (int k = 0; k <= i; k++) {
                if (k == 0 || k == i) {
                    list.add(1);
                } else {
                    list.add(pre.get(k - 1) + pre.get(k));
                }
            }
            pre = list;
        }
        return res;
    }

    @Test
    public void test1() {
        List<List<Integer>> result = new YangHui().generate(5);
        result.forEach(System.out::println);
    }
}
